package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
@Service
@Transactional
public class CheckGroupService extends ServiceImpl<CheckGroupMapper, CheckGroup> implements com.itheima.health.service.CheckGroupService {

    @Autowired
    private  CheckItemService checkItemService;



    @Override
    public Boolean saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        Integer id = checkGroupDTO.getId();
        if(StringUtils.isNotBlank(String.valueOf(id))){
            //  id不为空，根据 id  删除 中间表数据（修改）
            baseMapper.deleteCheckItemInfoByGroupId(id);
        }
        //id 为空 保存前端传递的数据（新建）
        boolean flag = saveOrUpdate(checkGroupDTO);
        Integer gid = checkGroupDTO.getId();
        //录入中间表
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.length > 0) {
            //  中间表循环录入
            for (Integer checkitemId : checkitemIds) {
                //   添加和修改  中间数据录入
                baseMapper.addCheckGroupIdAndCheckItemId(gid,checkitemId);

            }
        }
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //   select  * from xxx where is_delete = 0  and  xxx like ?
        //  条件构造出来   Wrapper对象
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();
        queryWrapper.eq("is_delete",0);
        Page<CheckGroup> page = null;
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }else{
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            //   sql   + 追加  sql  where  name like  %?%
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult  result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public Map findCheckItemInfoByGroupId(int id) {
        return null;
    }

    @Override
    public CheckGroupVO findChecked(Integer id) {
        List<CheckItem> list = checkItemService.list();
        int[] checkItemInfoByGroupId = baseMapper.findCheckItemInfoByGroupId(id);
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(list);
        checkGroupVO.setCheckitemIds(checkItemInfoByGroupId);
        return checkGroupVO;
    }
}
